{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "战斗 => 施法  ， 结果 \n",
      "施法 => 主语 动作 技能 \n",
      "结果 => 主语 获得 效果\n",
      "主语 => 张飞 | 关羽 | 赵云 | 典韦 | 许褚 | 刘备 | 黄忠 | 曹操 | 鲁班七号 | 貂蝉\n",
      "动作 => 施放 | 使用 | 召唤 \n",
      "技能 => 一骑当千 | 单刀赴会 | 青龙偃月 | 刀锋铁骑 | 黑暗潜能 | 画地为牢 | 守护机关 | 狂兽血性 | 龙鸣 | 惊雷之龙 | 破云之龙 | 天翔之龙\n",
      "获得 => 损失 | 获得 \n",
      "效果 => 数值 状态\n",
      "数值 => 1 | 1000 |5000 | 100 \n",
      "状态 => 法力 | 生命\n",
      " <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 定语从句语法\n",
    "grammar = '''\n",
    "战斗 => 施法  ， 结果 \n",
    "施法 => 主语 动作 技能 \n",
    "结果 => 主语 获得 效果\n",
    "主语 => 张飞 | 关羽 | 赵云 | 典韦 | 许褚 | 刘备 | 黄忠 | 曹操 | 鲁班七号 | 貂蝉\n",
    "动作 => 施放 | 使用 | 召唤 \n",
    "技能 => 一骑当千 | 单刀赴会 | 青龙偃月 | 刀锋铁骑 | 黑暗潜能 | 画地为牢 | 守护机关 | 狂兽血性 | 龙鸣 | 惊雷之龙 | 破云之龙 | 天翔之龙\n",
    "获得 => 损失 | 获得 \n",
    "效果 => 数值 状态\n",
    "数值 => 1 | 1000 |5000 | 100 \n",
    "状态 => 法力 | 生命\n",
    "'''\n",
    "print(grammar,type(grammar))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 得到语法字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "linesplit: \n",
      " <class 'str'>\n",
      "gramsplit: => <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "linesplit = \"\\n\"\n",
    "gramsplit = \"=>\"\n",
    "print(\"linesplit:\",linesplit,type(linesplit))#linesplit是换行 \n",
    "print(\"gramsplit:\",gramsplit,type(gramsplit))#gramsplit是=>   两个数据类型都是str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['',\n",
       " '战斗 => 施法  ， 结果 ',\n",
       " '施法 => 主语 动作 技能 ',\n",
       " '结果 => 主语 获得 效果',\n",
       " '主语 => 张飞 | 关羽 | 赵云 | 典韦 | 许褚 | 刘备 | 黄忠 | 曹操 | 鲁班七号 | 貂蝉',\n",
       " '动作 => 施放 | 使用 | 召唤 ',\n",
       " '技能 => 一骑当千 | 单刀赴会 | 青龙偃月 | 刀锋铁骑 | 黑暗潜能 | 画地为牢 | 守护机关 | 狂兽血性 | 龙鸣 | 惊雷之龙 | 破云之龙 | 天翔之龙',\n",
       " '获得 => 损失 | 获得 ',\n",
       " '效果 => 数值 状态',\n",
       " '数值 => 1 | 1000 |5000 | 100 ',\n",
       " '状态 => 法力 | 生命',\n",
       " '']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grammar.split(linesplit)#grammar是str类型 ，按照linesplit即换行符分割 分割之后 形成一个list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'战斗': [['施法', '，', '结果']],\n",
       " '施法': [['主语', '动作', '技能']],\n",
       " '结果': [['主语', '获得', '效果']],\n",
       " '主语': [['张飞'],\n",
       "  ['关羽'],\n",
       "  ['赵云'],\n",
       "  ['典韦'],\n",
       "  ['许褚'],\n",
       "  ['刘备'],\n",
       "  ['黄忠'],\n",
       "  ['曹操'],\n",
       "  ['鲁班七号'],\n",
       "  ['貂蝉']],\n",
       " '动作': [['施放'], ['使用'], ['召唤']],\n",
       " '技能': [['一骑当千'],\n",
       "  ['单刀赴会'],\n",
       "  ['青龙偃月'],\n",
       "  ['刀锋铁骑'],\n",
       "  ['黑暗潜能'],\n",
       "  ['画地为牢'],\n",
       "  ['守护机关'],\n",
       "  ['狂兽血性'],\n",
       "  ['龙鸣'],\n",
       "  ['惊雷之龙'],\n",
       "  ['破云之龙'],\n",
       "  ['天翔之龙']],\n",
       " '获得': [['损失'], ['获得']],\n",
       " '效果': [['数值', '状态']],\n",
       " '数值': [['1'], ['1000'], ['5000'], ['100']],\n",
       " '状态': [['法力'], ['生命']]}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gramdict = {}#\n",
    "for line in grammar.split(linesplit):\n",
    "    # 去掉首尾空格后，如果为空则退出\n",
    "    if not line.strip(): \n",
    "        continue\n",
    "    expr, statement = line.split(gramsplit)#按照gramsplit即\"=>\"进行分割 \n",
    "    #分割之后再放入一个字典离 key是=>前的内容  value是=>之后的句子内容 并且value再按照“|”分割形成的列表\n",
    "    gramdict[expr.strip()] = [i.split() for i in statement.split(\"|\")]\n",
    "gramdict    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 生成句子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成句子\n",
    "def generate(gramdict, target, isEng = False):\n",
    "    if target not in gramdict: \n",
    "        return target\n",
    "    find = random.choice(gramdict[target])\n",
    "    #print(find)\n",
    "    blank = ''\n",
    "    # 如果是英文中间间隔为空格\n",
    "    if isEng: \n",
    "        blank = ' '\n",
    "    return blank.join(generate(gramdict, t, isEng) for t in find)#这里运用了递归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "黄忠召唤刀锋铁骑，张飞损失5000法力\n",
      "刘备 使用 守护机关 ， 关羽 损失 5000 生命\n"
     ]
    }
   ],
   "source": [
    "print(generate(gramdict,\"战斗\"))\n",
    "print(generate(gramdict,\"战斗\", True))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
